package com.persondemo.videoappliction.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.VideoBean;
import com.persondemo.videoappliction.utils.T;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/23.
 */

public class XianLuAdapter extends RecyclerView.Adapter<XianLuAdapter.SimHodler> {
    private List<VideoBean.DataBean.XianluBean> list;
    Context context;
    private LayoutInflater mInflater;
    private MoviePopAdapter.OnItemActionListener mOnItemActionListener;
    private List<Boolean> isClicks;//控件是否被点击,默认为false，如果被点击，改变值，控件根据值改变自身颜色

    public XianLuAdapter(List<VideoBean.DataBean.XianluBean> list, Context context) {
        this.list = list;
        this.context = context;
        mInflater = LayoutInflater.from(context);
        isClicks = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            isClicks.add(false);
        }
    }

    @Override
    public SimHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mInflater.inflate(R.layout.item_xianlu, null, false);
        XianLuAdapter.SimHodler holder = new XianLuAdapter.SimHodler(v);
        holder.setIsRecyclable(true);
        return holder;
    }

    @Override
    public void onBindViewHolder(SimHodler holder, int position) {
        holder.xianlu_btn.setText(list.get(position).getName());
        if (isClicks.get(position)) {
            holder.xianlu_btn.setBackgroundResource(R.drawable.item_a);
        } else {
            holder.xianlu_btn.setBackgroundResource(R.drawable.item_b);
        }
        if (mOnItemActionListener != null) {
            holder.xianlu_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemActionListener.onItemClickListener(v, holder.getPosition());
                    for (int i = 0; i < isClicks.size(); i++) {
                        isClicks.set(i, false);
                    }
                    isClicks.set(position, true);
                    notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface OnItemActionListener {
        void onItemClickListener(View v, int pos);
    }

    public void setOnItemActionListener(MoviePopAdapter.OnItemActionListener onItemActionListener) {
        this.mOnItemActionListener = onItemActionListener;
    }

    public class SimHodler extends RecyclerView.ViewHolder {
        private Button xianlu_btn;
        public SimHodler(View itemView) {
            super(itemView);
            xianlu_btn = itemView.findViewById(R.id.xianlu_btn);
        }
    }
}
